<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$user_id = $user->getUserId();
$user_ip = $_SERVER['REMOTE_ADDR'];
$rumor = UV::readInt(strip_tags($route->POST("rumor")));
$mean = UV::readMQ(strip_tags($route->POST("mean")));

if (!isset($_COOKIE['_sess']) || $_COOKIE['_sess'] == ""){
	$gen_cookie = md5(randString().time());
	setcookie("_sess",$gen_cookie,strtotime("01.01.2015"),"/");
}
if (!isset($_COOKIE['_sess']))
	die("user");

$cookie = $_COOKIE['_sess'];
// проверяем - не голосовал ли уже пользователь
$query = $user_id ? "select count(`rl_id`) from `rumors_likes` where `rumor_id` = '$rumor' and (`user_id` = '$user_id' or `user_ip` = '$user_ip' or `_sess` = '$cookie')" : "select count(`rl_id`) from `rumors_likes` where `rumor_id` = '$rumor' and (`user_ip` = '$user_ip' or `_sess` = '$cookie')";
$rumors = $db->getCell($query);

if ($rumors != "0")
	die("user");

$time = time();

$db->sendQuery("insert into `rumors_likes` values (NULL,'$rumor','$mean','$user_id','$user_ip','$cookie','$time')");

$like = $db->getCell("select count(`rl_id`) from `rumors_likes` where `rumor_id` = '$rumor' and `mean` = 'like'");
$dislike = $db->getCell("select count(`rl_id`) from `rumors_likes` where `rumor_id` = '$rumor' and `mean` = 'dislike'");

echo '{"like":'.$like.',"dislike":'.$dislike.'}';

?>